﻿@page "/scheduler/export-and-import-ics"

@using Syncfusion.Blazor.Schedule
@using Syncfusion.Blazor.Inputs
@using Syncfusion.Blazor.Buttons
@using System.IO
@inherits SampleBaseComponent;

<SampleDescription>
    <p>This example showcases how to export the Scheduler events to a calendar (.ics) file, as well as how to import events from an .ics file (downloaded from any of the calendars like Google or Outlook) into our Scheduler. </p>
</SampleDescription>
<ActionDescription>
    <p>
        In this example, the Scheduler events can be exported to a calendar (.ics) file by using the
        <code>exportToICalendar</code> public method. By default, the calendar is exported with a file name <code>Calendar.ics</code>.
        To change this file name on export, pass the custom string value as <code>fileName</code> to get the file
        downloaded with this provided name. The events from external calendars can also be imported into Scheduler by using the <code>importICalendar</code> method.
    </p>
</ActionDescription>

<div class="col-lg-9 control-section">
    <div class="schedule-wrapper">
        <SfSchedule @ref="ScheduleRef" TValue="ScheduleData.AppointmentData" Width="100%" Height="650px" @bind-SelectedDate="@CurrentDate">
            <ScheduleEventSettings DataSource="@DataSource"></ScheduleEventSettings>
            <ScheduleViews>
                <ScheduleView Option="View.Day"></ScheduleView>
                <ScheduleView Option="View.Week"></ScheduleView>
                <ScheduleView Option="View.WorkWeek"></ScheduleView>
                <ScheduleView Option="View.Month"></ScheduleView>
                <ScheduleView Option="View.Agenda"></ScheduleView>
            </ScheduleViews>
        </SfSchedule>
    </div>
</div>
<div class="col-lg-3 property-section">
    <div class="property-panel-section">
        <div class="property-panel-header">Properties</div>
        <div class="property-panel-content">
            <table id='property' title='Properties' class='property-panel-table' style="width: 100%;">
                <tbody>
                    <tr style="height: 50px">
                        <td style="width: 30%">
                            <div>
                                Export as iCalendar
                            </div>
                        </td>
                        <td style="width: 50%;">
                            <div>
                                <SfButton Content="Export" OnClick="ExportToIcs"></SfButton>
                            </div>
                        </td>
                    </tr>
                    <tr style="height: 50px">
                        <td style="width: 30%">
                            <div>
                                Import iCalendar file
                            </div>
                        </td>
                        <td style="width: 50%;">
                            <div>
                                <SfUploader AllowedExtensions=".ics" CssClass="calendar-import" Multiple="false">
                                    <UploaderButtons Browse="Choose File"></UploaderButtons>
                                    <UploaderEvents ValueChange="OnChange"></UploaderEvents>
                                </SfUploader>
                            </div>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>
<style>
    .calendar-import.e-upload {
        border: 0;
        padding-left: 0 !important;
    }
    .calendar-import.e-upload .e-file-select-wrap {
        padding: 0
    }
    .calendar-import.e-upload .e-file-select-wrap .e-file-drop, .calendar-import .e-upload-files {
        display: none;
    }
</style>

@code{
    private DateTime CurrentDate = new DateTime(2020, 1, 9);
    SfSchedule<ScheduleData.AppointmentData> ScheduleRef;
    public List<ScheduleData.AppointmentData> DataSource = new ScheduleData().GetScheduleData();
    public async void ExportToIcs()
    {
        await ScheduleRef.ExportToICalendarAsync();
    }
    public async Task OnChange(UploadChangeEventArgs args)
    {
        foreach (var file in args.Files)
        {
            file.Stream.Position = 0;
            StreamReader reader = new StreamReader(file.Stream);
            await ScheduleRef.ImportICalendar(reader.ReadToEnd());
        }
    }
}